/**
 * 
 */
package org.statewalker.parser.wiki.tokenizer.block;

import org.statewalker.parser.wiki.tokenizer.base.StreamToken;
import org.statewalker.tokenizer.base.AbstractTokenizer;
import org.statewalker.tokenizer.base.ITokenFactory;
import org.statewalker.tokenizer.stream.ICharStream;
import org.statewalker.tokenizer.stream.ICharStream.IMarker;

public class ListTokenizer extends AbstractTokenizer<StreamToken> {

    public ListTokenizer(ITokenFactory<StreamToken> factory) {
        super(factory);
    }

    protected boolean isListMarker(char ch) {
        return ch == '*' || ch == '#';
    }

    protected boolean isSpace(char ch) {
        return ch == ' ' || ch == '\t';
    }

    public StreamToken read(ICharStream stream) {
        boolean result = false;
        IMarker marker = stream.markPosition();
        try {
            char ch = stream.getChar();
            while (isSpace(ch)) {
                if (!stream.incPos())
                    return null;
                ch = stream.getChar();
            }
            while (isListMarker(ch)) {
                result = true;
                stream.incPos();
                ch = stream.getChar();
            }
            if (!result)
                return null;
            StreamToken token = newToken(stream, marker);
            return token;
        } finally {
            marker.close(!result);
        }
    }
}